package com.ucomm.talent.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.ucomm.talent.AppConfig;
import com.ucomm.talent.AppConstants;
import com.ucomm.talent.R;
import com.ucomm.talent.activity.IHeaderBarClickListener;
import com.ucomm.talent.common.DebugUtil;

/**
 * 界面上最上放的Bar栏
 * Created with IntelliJ IDEA.
 * User: Vita Hao
 * Date: 13-5-22
 * Time: 上午11:29
 * To change this template use File | Settings | File Templates.
 */
public class MainHeaderBar extends RelativeLayout implements View.OnClickListener {

    private String headTitle;
    private RelativeLayout exitBackHeadLayout;
    private LinearLayout normalLayout;
    private View view;
    private Button back;
    private Button exit;
    private int status;
    private TextView headTitleView;
    private IHeaderBarClickListener barClickListener;

    public MainHeaderBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater = LayoutInflater.from(context);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.mainHeaderBar);
        this.status = a.getInt(R.styleable.mainHeaderBar_headState, AppConstants.MainHeaderBarStatus.NONE);
        this.headTitle = a.getString(R.styleable.mainHeaderBar_headTitle);
        
        view = inflater.inflate(R.layout.wel_top_bar_layout, null);
        exitBackHeadLayout = (RelativeLayout) view.findViewById(R.id.exit_back_head_layout);
        normalLayout = (LinearLayout) view.findViewById(R.id.normal_layout);
        this.back = (Button) exitBackHeadLayout.findViewById(R.id.bt_back);
        this.back.setOnClickListener(this);
        this.exit = (Button) exitBackHeadLayout.findViewById(R.id.bt_exit);
        this.exit.setOnClickListener(this);
        this.headTitleView = (TextView) exitBackHeadLayout.findViewById(R.id.head_title);
        headTitleView.setText(headTitle);
        handleStatus(status);

        LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        this.addView(view, lp);


    }

    private void handleStatus(int status) {
        if (status == AppConstants.MainHeaderBarStatus.NONE) {
            showNormalLayout();
        } else if (status == AppConstants.MainHeaderBarStatus.BACK_AND_EXIT) {
            showExitBackLayout();
        } else {
            showNotExitBackLayout();
        }
    }

    public void showNormalLayout() {
        if (view != null && normalLayout != null) {
            normalLayout.setVisibility(View.VISIBLE);
        }
        if (exitBackHeadLayout != null) {
            exitBackHeadLayout.setVisibility(View.GONE);
        }
    }

    public void showExitBackLayout() {
        if (view != null && exitBackHeadLayout != null) {
            exitBackHeadLayout.setVisibility(View.VISIBLE);
            exit.setVisibility(View.VISIBLE);
            back.setVisibility(View.VISIBLE);
        }
        if (normalLayout != null) {
            normalLayout.setVisibility(View.GONE);
        }
    }

    public void showNotExitBackLayout() {
        if (view != null && exitBackHeadLayout != null) {
            exitBackHeadLayout.setVisibility(View.VISIBLE);
            back.setVisibility(View.VISIBLE);
            exit.setVisibility(View.GONE);
        }
        if (normalLayout != null) {
            normalLayout.setVisibility(View.GONE);
        }
    }

    public void setBarClickListener(IHeaderBarClickListener barClickListener) {
        this.barClickListener = barClickListener;
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == exit.getId()) {
            if(barClickListener != null){
                barClickListener.onExitButtonOnClick(view);
            }
            DebugUtil.Log(MainHeaderBar.this, "------exit");
        } else if (id == back.getId()) {
            if(barClickListener != null){
                barClickListener.onBackButtOnClick(view);
            }
            DebugUtil.Log(MainHeaderBar.this, "------back");
        }
    }
}
